package at.pp.match3.game;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.view.SurfaceHolder;
import at.pp.match3.game.AbstractEngine.GameState;

public class GameThread extends Thread {

    private Context context;
    private SurfaceHolder surfaceHolder;
    private Handler handler;
    private boolean isGameRunning;

    private AbstractEngine engine;

    @Override
    public final void run() {
        while (isGameRunning) {
            Canvas c = null;
            try {
                c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    if (engine.getGameState() == GameState.Running) {
                        engine.updatePhysics();
                    }
                    engine.renderScene(c);
                }
            } finally {
                if (c != null) {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }

    public final AbstractEngine getEngine() {
        return engine;
    }

    public final void setEngine(final AbstractEngine engine) {
        this.engine = engine;
    }

    public final void setContext(final Context context) {
        this.context = context;
    }

    public final Context getContext() {
        return context;
    }

    public final void setSurfaceHolder(final SurfaceHolder surfaceHolder) {
        this.surfaceHolder = surfaceHolder;
    }

    public final SurfaceHolder getSurfaceHolder() {
        return surfaceHolder;
    }

    public final void setHandler(final Handler handler) {
        this.handler = handler;
    }

    public final Handler getHandler() {
        return handler;
    }

    public final boolean isGameRunning() {
        return isGameRunning;
    }

    public final void setGameRunning(final boolean isGameRunning) {
        this.isGameRunning = isGameRunning;
    }

}
